# coding: utf-8
from random import randint

from smart_qq_bot.messages import GroupMsg, PrivateMsg
from smart_qq_bot.signals import on_all_message, on_bot_inited
from smart_qq_bot.logger import logger
import linecache
import random


@on_bot_inited("PluginManager")
def manager_init(bot):
    logger.info("讲笑话插件开始运行啦！:)")

@on_all_message(name="SamplePlugin")
def sample_plugin(msg, bot):
    """
    :type bot: smart_qq_bot.bot.QQBot
    :type msg: smart_qq_bot.messages.GroupMsg
    """
    msg_id = randint(1, 10000)
    # 发送一条群消息
    if "荤段子" in msg.content:
        if isinstance(msg, GroupMsg):
            # 获取文件有多少行
            count = len(open(r"joking.txt", 'rU').readlines())
            logger.info(count)
            a = random.randrange(1, count)
            theline = linecache.getline(r'joking.txt', a).decode("GBK")
            bot.send_group_msg(theline, msg.from_uin, msg_id)
        # 发送一条私聊消息
        elif isinstance(msg, PrivateMsg):
            count = len(open(r"joking.txt", 'rU').readlines())
            a = random.randrange(1, count)
            theline = linecache.getline(r'joking.txt', a).decode("GBK")
            bot.send_friend_msg(theline, msg.from_uin, msg_id)

    if "陆瑶" in msg.content:
        if isinstance(msg, GroupMsg):
            bot.send_group_msg("陆瑶最可爱", msg.from_uin, msg_id)

    if "!菜单" in msg.content:
        if isinstance(msg, GroupMsg):
            bot.send_group_msg("现有功能：荤段子", msg.from_uin, msg_id)